/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.utils;

import cn.ac.nercis.pes.common.constant.project.SystemState;
import cn.ac.nercis.pes.model.dto.common.DataCopy;
import cn.ac.nercis.pes.model.dto.common.DataPaste;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 数据复制辅助类
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public final class DataCopyUtils {
    private static final ObjectProperty<DataCopy> COPY_CLIPBOARD = new SimpleObjectProperty<>();

    private DataCopyUtils(){}

    public static void copyClipboard(DataCopy dataCopy){
        COPY_CLIPBOARD.set(dataCopy);
    }

    public static DataCopy getClipboard() {
        return COPY_CLIPBOARD.get();
    }

    public static boolean hasClipboard(String name, SystemState state) {
        if (Objects.isNull(COPY_CLIPBOARD.get())) {
            return false;
        }
        return StringUtils.equals(COPY_CLIPBOARD.get().getName(), name) && COPY_CLIPBOARD.get().getState() == state;
    }

    public static boolean hasClipboard(DataPaste paste, SystemState state){
        if (Objects.isNull(COPY_CLIPBOARD.get())) {
            return false;
        }
        if(StringUtils.isEmpty(paste.getData())){
            return StringUtils.equals(COPY_CLIPBOARD.get().getName(), paste.getName())
                    && COPY_CLIPBOARD.get().getState() == state
                    && StringUtils.isEmpty(COPY_CLIPBOARD.get().getData());
        }else{
            return StringUtils.equals(COPY_CLIPBOARD.get().getName(), paste.getName())
                    && COPY_CLIPBOARD.get().getState() == state
                    && StringUtils.isNotBlank(COPY_CLIPBOARD.get().getData());
        }
    }

    public static boolean hasClipboard(String name, String data, SystemState state) {
        if (Objects.isNull(COPY_CLIPBOARD.get())) {
            return false;
        }
        return StringUtils.equals(COPY_CLIPBOARD.get().getName(), name) && StringUtils.equals(COPY_CLIPBOARD.get().getData(), data) && COPY_CLIPBOARD.get().getState() == state;
    }
}